<?php

namespace app\common\model;

use think\Controller;
use think\Model;
use think\captcha\Captcha;
use think\Db;
class Merchant extends Model
{
	
	public static function makeCaptcha()
	{
		$model = new Captcha([
			 // 验证码字符集合
	        'codeSet' => '0123456789',
	        // 验证码字体大小(px)
	        'fontSize' => 25,
	        // 是否画混淆曲线
	        'useCurve' => false,
	        // 验证码图片高度
	        'imageH' => 50,
	        // 验证码图片宽度
	        'imageW' => 180,
	        // 验证码位数
	        'length' => 4,
	        // 验证成功后是否重置
	        'reset' => true
		]);
		return $model->entry();
	}  
	// 激活商户账户
	public static function activeMerchantAccount()
	{
		$merchant_id = session('merchant.merchant_id');
	 	Db::transaction(function() use ($merchant_id) {
	        Store::where('store_merchant_id', $merchant_id)->setField('store_open_time', time());
		    Merchant::where('merchant_id', $merchant_id)->setField('merchant_account_status', '1');
        });
        return true;
	}
 	public static function updatePwd($phone, $password)
 	{
 		$result = Merchant::where('merchant_tel', $phone)->setField('merchant_password', encryption_pwd($password));
 		return $result;
 	}


 	public static function regist($phone, $password)
	{
		$result = Merchant::create([
			'merchant_tel' => $phone,
			'merchant_reg_time' => time(),
			'merchant_password' => encryption_pwd($password)
		]);
		return $result;
 	} 

	public static function getMerchant($phone) 
	{
		$merchant = Merchant::where('merchant_tel', $phone)->where('merchant_is_delete',0)->find();
		return $merchant;
	}


	public static function logout()
	{
		session('merchant', null);
		cookie('phone', null);
		return redirect('merchant/login');
	}



	// 查看公司和企业的审核状态
	public static function checkStatus()
	{
		$merchant_id = session('merchant.merchant_id');

		$store = db('store')->field('store_id,store_verify_status')->where('store_merchant_id', $merchant_id)->find();
		$enterprise = db('enterprise')->field('enterprise_id,enterprise_verify_status')->where('enterprise_merchant_id', $merchant_id)->find();

		$data['store_verify_status'] = $store['store_verify_status'];
		$data['store_id'] = $store['store_id'];
		$data['enterprise_verify_status'] = $enterprise['enterprise_verify_status'];
		$data['enterprise_id'] = $enterprise['enterprise_id'];
		return $data;
	}  
	// 运营前台获取商户注册资料
	public static function getRgisterMerchant($pagesize)
	{

		$where['a.merchant_is_delete'] = ['eq', 0];
		$keyword = input('param.keyword');
		if($keyword) 
		{
			$where['a.merchant_tel|b.enterprise_name|c.store_name'] = ['like', "%$keyword%"];
 		}

		$data = Merchant::alias('a')->field('a.merchant_id,a.merchant_account_status,a.merchant_tel,b.enterprise_name,b.enterprise_id,c.store_id,c.store_name')
			-> join('enterprise b', 'a.merchant_id=b.enterprise_merchant_id', 'left')
			-> join('store c', 'a.merchant_id=c.store_merchant_id', 'left')
			-> where($where)
			-> order('a.merchant_id', 'desc')
			-> paginate($pagesize);
		return $data;
	}

	// 删除
	public static function changeDel($id)
	{
		$result = Merchant::where('merchant_id', $id)->setField('merchant_is_delete', 1);
		if(!$result)
			return false;

		return true;

	}
 
	public function getById($id)
	{
		$res = Merchant::where('merchant_id', $id)->find();
		return $res;
	}

	public function updateById($id,$data)
	{
		$res = Merchant::where('merchant_id', $id)->update($data);
		return $res;
	}

	// // 发送短信：接口未申请，代码先注释
	// public static function sendSMS(Request $request, $phone)
	// {
		
	// 	$charset = '1234567890';
	// 	$code = '';
	// 	$_len = strlen($charset) - 1;
	// 	for ($i=0; $i < 6; $i++) { 
	// 		$code .= $charset[mt_rand(0, $_len)];
	// 	}
	// 	$result = sendTemplateSMS($phone, [$code, 60], 1);
	// 	$tempphone = model('temp_phone');
	// 	$tempphone->phone = $phone;
	// 	$tempphone->code = $code;
	// 	$tempphone->deadline = date('Y-m-d H:i:s', time() + 3600);
	// 	if(!$tempphone -> save())
	// 		return false;
		
	// 	return true;
	// }
	// 
	// public function registerDo()
	// {
	// 	$phone = input('param.phone');
	// 	$phone_code = input('param.phone_code');
	// 	$password = input('param.password');
	// 	if($phone_code == '' || strlen($phone_code) !== 6)
	// 		return toJson(0, '手机验证码为6位');

	// 	$tempModel = model('temp_phone');
	// 	$maxId = $tempModel->max('id');
	// 	$tempPhone = $tempModel->where([
	// 		'phone' => $phone,
	// 		'id' => $maxId
	// 	]) -> find(); 
	// 	if($tempPhone->code != $phone_code || (time() > strtotime($tempPhone->deadline)))
	// 	{
	// 			return returnJson(0, '手机验证码不正确');
	// 	}
	// 	$merchant = new Merchant;
	// 	$merchant->merchant_tel = $phone;
	// 	$merchant->merchant_password = entryption_password($password);
	// 	if(!$merchant->save())
	// 		 returnJson(0, '注册失败，请稍后重试！');


	// 	return returnJson(1, '注册成功！') ;
       	
	// }     
}
